/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class CustomerServiceI implements CustomerService {

    private CustomerI ci;
    private CustomerConnection conn = new CustomerConnection();
    private Statement st = null;
    private ResultSet rs = null;

    public CustomerI findByUser(CustomerI ci) {
        this.ci = new CustomerI();
        this.dbConnection();
        this.getResultSet("SELECT * FROM customer WHERE user='" + ci.getUser() + "'");
        try {
            this.ci.setId(rs.getInt(1));
            this.ci.setUser(rs.getString("user"));
            this.ci.setEmail(rs.getString("email"));
            this.ci.setPassword(rs.getString("password"));
            this.ci.setTel(rs.getString("tel"));
            this.ci.setLocation(rs.getString("location"));
            this.ci.setPeople(rs.getString("people"));
            return this.ci;
        } catch (SQLException ex) {
            this.ci = null;
            System.out.println("Method CustomerServiceI.findByUser() failure!");
            ex.printStackTrace();
            return this.ci;
        }

    }

    public void insertCustomer(CustomerI ci) {
        this.dbConnection();
        this.exeInsert("Insert Into customer(id,user,password,email,tel,location,people) Values(" + ci.getId() + ",'" + ci.getUser() + "','" + ci.getPassword() + "','" + ci.getEmail() + "','" + ci.getTel() + "','" + ci.getLocation()+ "','" + ci.getPeople()+ "')");
    }

    public void dbConnection() {
        try {
            conn.Load_DB();
            conn.Connection_DB();
        } catch (ClassNotFoundException ex) {
            System.out.println("Class Not Found!");
            ex.printStackTrace();
        } catch (SQLException sqle) {
            System.out.println("Connection Dababase failure!");
            sqle.printStackTrace();
        }
    }

    public void getResultSet(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    rs = st.executeQuery(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method CustomerServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public void exeInsert(String sql) {
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method CustomerServiceI.getResultSet() failure!");
            sqle.printStackTrace();
        }
    }

    public List findAll() {
        CustomerI cu;
        ArrayList list = new ArrayList();
        this.dbConnection();
        this.getResultSet("SELECT * FROM customer");
        try {
            while (rs.next()) {
                cu = new CustomerI();
                cu.setId(rs.getInt(1));
                cu.setUser(rs.getString("user"));
                cu.setEmail(rs.getString("email"));
                cu.setPassword(rs.getString("password"));
                cu.setTel(rs.getString("tel"));
                cu.setLocation(rs.getString("location"));
                cu.setPeople(rs.getString("people"));
                list.add(cu);
            }
        } catch (SQLException ex) {
            System.out.println("Method CustomerServiceI.findAll() failure!");
            ex.printStackTrace();
        }
        return list;
    }

    public void deleteCustomer(CustomerI ci) {
        this.dbConnection();
        this.exeDelete("Delete From customer Where user='" + ci.getUser()+"'");
    }

    public void exeDelete(String sql){
        try {
            if (conn != null) {
                st = conn.getConn().createStatement();
                if (st != null) {
                    st.executeUpdate(sql);
                }
            }
        } catch (SQLException sqle) {
            System.out.println("Method CustomerServiceI.exeDelete() failure!");
            sqle.printStackTrace();
        }
    }
}
